/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless.MRP.MaterialRequirement;

import entity.BillOfMaterial;
import java.util.List;
import javaclass.ProductType;
import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Vincent
 */
@Stateless
public class BillOfMaterialBean implements BillOfMaterialBeanLocal {

    @PersistenceContext (unitName = "M10-IS3102-ejbPU")
    private EntityManager entityManager; 
    
    @Override
    public BillOfMaterial findBillOfMaterial(ProductType productType, String materialType){
        Query query = entityManager.createQuery("SELECT b FROM BillOfMaterial b WHERE b.barType = :inBarType");
        query.setParameter("inBarType", productType);
        List<BillOfMaterial> billOfMaterials = null;
        try{
            billOfMaterials = query.getResultList();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        for (BillOfMaterial b:billOfMaterials){
            if (b.getRawMaterial().getMaterialType().equals(materialType)){
                return b;
            }
        }
        return null;

    }
    
    @Override
    public List<BillOfMaterial> findRecipe(ProductType productType){
        Query query = entityManager.createQuery("SELECT b FROM BillOfMaterial b WHERE b.BarType = :inBarType");
        query.setParameter("inBarType", productType);
        List<BillOfMaterial> billOfMaterials = null;
        try{
            billOfMaterials = query.getResultList();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return billOfMaterials;
    }
}
